package com.pww.movie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pww.movie.domain.DTO.MediaDto;
import com.pww.movie.domain.PO.Media;
import com.pww.movie.service.IMediaService;
import com.pww.movie.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 媒体资源表 前端控制器
 * </p>
 *
 * @author pww
 * @since 2025-06-09
 */
@Slf4j
@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private IMediaService mediaService;

    /**
     * 上传图片
     *
     * @param file 图片文件，必填
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file,
                         @RequestParam(required = true) String title,
                         @RequestParam(required = true) String type,
                         @RequestParam(required = true) Boolean isBanner,
                         @RequestParam(required = true) Integer createId,
                         @RequestParam(required = false) String description,
                         @RequestParam(required = false) String coverImage  // 添加封面参数
    ) {
        try {
            String url = mediaService.uploadBanner(file, title, type, isBanner, createId, description, coverImage);
            System.out.println("createId" + createId);
            return Result.success("上传成功，访问地址：" + url);
        } catch (Exception e) {
            log.error("上传失败：{}", e.getMessage());
            return Result.error("上传失败：" + e.getMessage());
        }
    }

    /**
     * 获取七张轮播图
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<Media>> list(@RequestParam Long createId) {
        List<Media> banners = mediaService.getLatestBannersWithUserLike(createId);
        return Result.success(banners);
    }

    /**
     * 获取用户喜欢的图片
     * 最多二十张
     *
     * @param userId
     */

    @GetMapping("/listUserLike")
    public Result<List<Media>> listUserLike(@RequestParam Long userId) {
        return mediaService.getUserLike(userId);
    }


    /**
     * 获取用户喜欢的视频
     */
    @GetMapping("/listUserLikeVideo")
    public Result<List<Media>> listUserLikeVideo(@RequestParam Long userId) {
        return mediaService.getUserLikeVideo(userId);
    }

    /**
     * 获取用户图片
     */

    @GetMapping("/listUserMedia")
    public Result<Page<MediaDto>> listUserMedia(@RequestParam(name = "current", defaultValue = "1") Integer pageNum,
                                                @RequestParam(name = "size", defaultValue = "10") Integer pageSize) {
        System.out.println("pageNum = " + pageNum + ", pageSize = " + pageSize);
        return mediaService.getUserPicture(pageNum, pageSize);
    }


    /**
     * 获取用户视频
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/listUserVideo")
    public Result<Page<MediaDto>> listUserVideo(@RequestParam(name = "current", defaultValue = "1") Integer pageNum,
                                             @RequestParam(name = "size", defaultValue = "10") Integer pageSize) {
        System.out.println("pageNum = " + pageNum + ", pageSize = " + pageSize);
        return mediaService.getUserVideo(pageNum, pageSize);
    }

}
